<script type="text/javascript">
	function SignUpCtrl($http, $injector, $rootScope, $element, $state, modalService) {
		var ctrl = this
		this.onDataLoad = function (data) {
			this.termData = data;
			var study = this.study = [];
			var applyed = this.applyed = [];
			var canApply = this.canApply = []
			angular.forEach(data, function (v) {
				if (v.stu && v.stu.length > 0) {
					study.push(v)
				} else if (v.apply && v.apply.length > 0) {
					applyed.push(v)
				} else {
					canApply.push(v)
				}

			})
		}
		this.applyTerm = function (term) {
			var tpl = $element.find('form.tpl').clone();
			var signInfo = { term: term };
			modalService.confirm({
				title: '报名',
				content: tpl,
				term: term,
				result: {},
				signInfo: signInfo
			}).then(function () {
				$http.post(':root/term/apply', signInfo).then(function (r) {
					modalService.succ('申请成功')
					$state.reload()
				})
			})
		}
	}

</script>
<div ng-controller="SignUpCtrl as ctrl" ng-init="ctrl.time=getCurrentDate()">
	<data entity="Term" auto-load="true" page="false" bind-ctrl="ctrl.terms" after-refresh="ctrl.onDataLoad(data)">
		<filter field="startTime" operator="<" value="ctrl.time"></filter>
		<filter field="endTime" operator=">" value="ctrl.time"></filter>
		<filter field="status" operator="=" value="1"></filter>
		<filter enable="$user.studentRole" field="trainType.forStudent" operator="=" value="true"></filter>
		<filter enable="$user.teacherRole" field="trainType.forTeacher" operator="=" value="true"></filter>
		<data entity="TermStuApply" slave-property="term.id" master-property="id" slave-as="apply">
			<filter field="user.id" value="$user.id" operator="=" />
		</data>
		<data entity="StuInfo" slave-property="term.id" master-property="id" slave-as="stu">
			<filter field="user.id" value="$user.id" operator="=" />
		</data>
	</data>

	<div>
		<h3 class="header container-fluid media">报名</h3>
		<div class="container-fluid media"></div>
		<div class="container-fluid media">
			<table class="table  table-hover media">
				<tr ng-repeat="t in ctrl.canApply">
					<td></td>
					<td>
						<h4>{{t.name}} {{t.trainType.name}} 正在开放报名</h4>
					</td>
					<td><a class="btn btn-success" ng-click="ctrl.applyTerm(t)">立即报名</a></td>
					<td></td>
				</tr>
				<tr ng-repeat="t in ctrl.applyed">
					<td></td>
					<td>
						<h4>{{t.name}} {{t.trainType.name}} </h4>
					</td>
					<td>
						<h4>{{t.apply[0].statusName}}</h4</td>
							<td></td>
				</tr>
				<tr ng-repeat="s in stuInfos">
					<td></td>
					<td>
						<h4>{{s.term.name}} {{s.term.trainType.name}} </h4>
					</td>
					<td>
						<h4 class="text-success">{{s.term.status?'正在学习':'学习结束'}}</h4>
					</td>
					<td></td>
				</tr>

			</table>
			<div class="text-center h4" ng-show="ctrl.termData.length==0">
				<hr>当前没有可报名的培训班</div>
		</div>
		<div ng-non-bindable="true" class="hidden">
			<form class="form-horizontal media tpl">
				<div class="form-group">
					<label for="inputEmail3" class="col-sm-2 control-label">报名期次</label>
					<div class="col-sm-10">
						<input type="text" readonly="readonly" class="form-control" ng-value="term.name+' '+term.trainType.name">
					</div>
				</div>

				<data entity="TermIntro" auto-load="true" after-refresh="intro=data[0].intro">
					<filter field="term" value="term.id"></filter>
				</data>
				<div class="form-group">
					<label for="inputEmail3" class="col-sm-2 control-label">简介</label>
					<div class="col-sm-10">
						<textarea type="text" title="{{intro}}" readonly="readonly" style="resize:none" rows="4" class="form-control" ng-model="intro"></textarea>
					</div>
				</div>
				<div class="form-group">
					<label for="inputEmail3" class="col-sm-2 control-label">姓名</label>
					<div class="col-sm-10">
						<input type="text" readonly="readonly" class="form-control" ng-model="$user.name">
					</div>
				</div>
				<div class="form-group">
					<label for="inputEmail3" class="col-sm-2 control-label">姓别</label>
					<div class="col-sm-10">
						<label class="radio-inline">
							<input type="radio" required="required" ng-model="signInfo.sex" name="inlineRadioOptions"  value="0"/> 男
						</label>
						<label class="radio-inline">
							<input type="radio" required="required"  ng-model="signInfo.sex"  name="inlineRadioOptions"  value="1"/>女
						</label>
					</div>
				</div>

				<!--<div class="form-group">
					<label for="inputEmail3" class="col-sm-2 control-label">性别</label>
					<div class="col-sm-10">
						<input type="text" readonly="readonly" class="form-control" ng-model="$student.sexString">
					</div>
				</div>-->
				<div class="form-group">
					<label for="inputEmail3" class="col-sm-2 control-label">{{$user.teacherRole?'工号':'学号'}}</label>
					<data></data>
					<div class="col-sm-10">
						<input type="text" readonly="readonly" class="form-control" ng-model="$user.uid">
					</div>
				</div>
				<div class="form-group">
					<label for="inputEmail3" class="col-sm-2 control-label">院系</label>
					<data ng-if="term" entity="Department" after-refresh="result.department=data[0]" auto-load="true">
						<filter field="id" value="$user.department"></filter>
					</data>
					<div class="col-sm-10">
						<input type="text" readonly="readonly" class="form-control" ng-model="result.department.name">
					</div>
				</div>
				<div class="form-group" ng-if="term&&$user.studentRole">
					<label for="inputEmail3" class="col-sm-2 control-label">专业</label>
					<data ng-if="term&&$user.studentRole" entity="Major" after-refresh="result.major=data[0]" auto-load="true">
						<filter field="id" value="$user.major"></filter>
					</data>
					<div class="col-sm-10">
						<input type="text" readonly="readonly" class="form-control" ng-model="result.major.name">
					</div>
				</div>
				<div class="form-group" ng-if="term&&$user.studentRole">
					<label for="inputEmail3" class="col-sm-2 control-label">年级</label>
					<div class="col-sm-10">
						<input type="text" readonly="readonly" class="form-control" ng-model="$user.grade">
					</div>
				</div>
				<div class="form-group">
					<label for="inputEmail3" class="col-sm-2 control-label">所在校区</label>
					<div class="col-sm-10">
						<select class="form-control" ng-model="signInfo.xq" required="required">
						<option value="仙林校区">仙林校区</option>
						<option value="鼓楼校区">鼓楼校区</option>
					</select>
					</div>
				</div>
				<div class="form-group">
					<label for="inputEmail3" class="col-sm-2 control-label">手机</label>
					<div class="col-sm-10">
						<input type="text" class="form-control" required="required" ng-model="signInfo.phone">
					</div>
				</div>
				<div class="form-group">
					<label for="inputEmail3" class="col-sm-2 control-label">QQ</label>
					<div class="col-sm-10">
						<input type="text" class="form-control" required="required" ng-model="signInfo.qq">
					</div>
				</div>
				<div class="form-group">
					<label for="inputEmail3" class="col-sm-2 control-label">职务</label>
					<div class="col-sm-10">
						<input type="text" class="form-control" required="required" ng-model="signInfo.zw">
					</div>
				</div>
			</form>
		</div>
	</div>
</div>